package com.poly.ureport.controller;

import com.poly.common.core.domain.R;
import com.poly.common.core.web.controller.BaseController;
import com.poly.common.core.utils.poi.ExcelUtil;
import com.poly.common.core.validate.Add;
import com.poly.common.core.validate.Update;
import com.poly.common.core.web.page.Pagination;
import com.poly.common.core.web.page.TableData;
import com.poly.common.security.annotation.RequiresPermissions;
import com.poly.ureport.domain.SysReport;
import com.poly.ureport.pojo.dto.SysReportDTO;
import com.poly.ureport.pojo.query.SysReportQuery;
import com.poly.ureport.pojo.vo.SysReportVO;
import com.poly.ureport.service.ISysReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 报表Controller
 * 
 * @author poly
 * @date 2024-02-19
 */
@Api(tags = "报表")
@RestController
@RequestMapping("report")
public class SysReportController extends BaseController
{
    @Autowired
    private ISysReportService sysReportService;
    /**
     * 查询报表分页
     */
    @ApiOperation("查询报表分页")
    @RequiresPermissions("system:report:list")
    @GetMapping("/queryPage")
    public R<Pagination<SysReportVO>> queryPage(SysReportQuery query)
    {
        return R.ok(sysReportService.queryPage(query));
    }

    /**
     * 查询报表列表
     */
    @ApiOperation("查询报表列表")
    @RequiresPermissions("system:report:list")
    @GetMapping("/list")
    public R<List<SysReportVO>> list(SysReportQuery query)
    {
        return R.ok(sysReportService.queryList(query));
    }

    /**
     * 导出报表列表
     */
    @ApiOperation("导出报表列表")
    @RequiresPermissions("system:report:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysReportQuery query)
    {
        List<SysReportVO> list = sysReportService.queryList(query);
        ExcelUtil<SysReportVO> util = new ExcelUtil<SysReportVO>(SysReportVO.class);
        util.exportExcel(response, list, "报表数据");
    }

    /**
     * 获取报表详细信息
     */
    @ApiOperation("获取报表详细信息")
    @RequiresPermissions("system:report:query")
    @GetMapping(value = "/{id}")
    public R<SysReport> getInfo(@PathVariable("id") Long id)
    {
        return R.ok(sysReportService.get(id));
    }

    /**
     * 新增报表
     */
    @ApiOperation("新增报表")
    @RequiresPermissions("system:report:add")
    @PostMapping
    public R add(@RequestBody @Validated({Add.class}) SysReportDTO dto) {
        return toResult(sysReportService.add(dto));
    }

    /**
     * 修改报表
     */
    @ApiOperation("修改报表")
    @RequiresPermissions("system:report:edit")
    @PutMapping
    public R edit(@RequestBody @Validated({Update.class}) SysReportDTO dto) {
        return toResult(sysReportService.update(dto));
    }

    /**
     * 删除报表
     */
    @ApiOperation("删除报表")
    @RequiresPermissions("system:report:remove")
	@DeleteMapping("/{ids}")
    public R remove(@PathVariable Long[] ids)
    {
        return toResult(sysReportService.deleteByIds(ids));
    }
}
